package com.ydj;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;

@Component
public class AppEnvironment {
    @Autowired
    private Environment environment;

    @Getter
    private static String buildType;

    @Getter
	private String serverName;

    @Getter
    private String asyncUrl;

    @Getter
    private String thirdUrl;

    @Getter
    private String syncUrl;

    @Getter
    private String openAppId;

    @Getter
    private String componentSecret;

    @Getter
    private String componentToken;

    @Getter
    private String componentAesKey;

    private Boolean master;

    /**
     * @PostConstruct 会在Servlet构造函数之后，初始化之前执行
     */
    @PostConstruct
    public void init() {
        buildType = environment.getProperty("spring.profiles.active");
        serverName = environment.getProperty("config.server-name");
        asyncUrl = environment.getProperty("config.async-url");
        thirdUrl = environment.getProperty("third.url");
        syncUrl = environment.getProperty("sync.url");
        openAppId = environment.getProperty("wechat.open.componentAppId");
        componentToken = environment.getProperty("wechat.open.componentToken");
        componentSecret = environment.getProperty("wechat.open.componentSecret");
        componentAesKey = environment.getProperty("wechat.open.componentAesKey");
        String strMaster = environment.getProperty("master");
        if (StringUtils.isBlank(strMaster)) {
            master = true;
        } else {
            master = Boolean.parseBoolean(strMaster);
        }
    }

    public String getPropertyString(String key) {
        return environment.getProperty(key);
    }

    public Integer getPropertyInt(String key) {
        String value = getPropertyString(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return Integer.parseInt(value);
    }

    /**
     * 获取JAVA的临时目录
     */
    public File getJavaTempDirectory() {
        return new File(System.getProperty("java.io.tmpdir"));
    }

    public boolean isTest() {
        return buildType.startsWith("test");
    }

    public boolean isProd() {
        return buildType.startsWith("prod");
    }

	public boolean isMaster() {
		return master;
	}

}